package com.mdlaser.util;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @description:代码自动生成
 * @author: 张宁
 * @date: 2023/4/20 15:33
 * @param:
 * @return:
 **/
public class GeneratorUtil {
    public static void main(String[] args) {
        ArrayList<String> tableList = new ArrayList<>();
        tableList.add("sys_resource");
        String jdbc = "1.92.104.100:3306/mdlaser?characterEncoding=UTF-8&useSSL=false&verifyServerCertificate=false&rewriteBatchedStatements=true&serverTimezone=UTC";
        String user = "root";
        String pwd = "zxp@82339319";
        GeneratorUtil.generate(jdbc, user, pwd, null, tableList);

    }

    private static void generate(String jdbc, String userName, String passWord, String outPath, List<String> tableList) {
        //数据连接
        FastAutoGenerator fastAutoGenerator = FastAutoGenerator.create(
                jdbc, userName, passWord);
        if (!StringUtils.hasLength(outPath)) {
            String projectPath = System.getProperty("user.dir");
            outPath = projectPath + "\\codeGenerator\\src\\main\\java\\";
        }
        File directory = new File(outPath);

        // 如果文件夹不存在，则创建文件夹
        if (!directory.exists()) {
            directory.mkdirs();
        }

        String finalOutPath = outPath;
        fastAutoGenerator.globalConfig(builder -> {
                    builder.author("mdlaser") // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件(已经过时）
                            .enableSpringdoc()
                            .commentDate("yyyy-MM-dd")
                            .outputDir(finalOutPath); // 指定输出目录 *
                })
                .packageConfig(builder -> {
                    builder.parent("com.mdlaser") // 设置父包名 !!!
                            .moduleName(null) // 设置父包模块名
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.xml,
                                    finalOutPath + "\\com\\mdlaser\\mapper\\xml")); // 设置mapperXml生成路径 !!!
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableList) // 设置需要生成的表名 !!!
                            .addTablePrefix("sys_")// 设置过滤表前缀 !!!
                            .enableCapitalMode()
                            .serviceBuilder() //service生成策略
                            .formatServiceFileName("I%sService")  //service类名，%s适配，根据表明替换
                            .formatServiceImplFileName("%sServiceImpl")  // service实现类
                            .entityBuilder()
                            .naming(NamingStrategy.underline_to_camel)//实体类生成策略
                            .enableLombok()
                            //开启lombok
//                            .logicDeleteColumnName("deleted")  //说明逻辑删除是哪个字段
                            .enableTableFieldAnnotation()  // 属性加上说明注解
                            .controllerBuilder()  //controller生成策略
                            .enableHyphenStyle() //开启驼峰转连字符
                            .formatFileName("%sController")
                            .enableRestStyle()  //开启RestController
                            .mapperBuilder()
                            .superClass(BaseMapper.class)  //dao层继承父类
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation() //@Mapper注解开启
                            .formatXmlFileName("%sMapper");
                })


                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}